
<script>
	chrome.extension.onConnect.addListener(function(port){
		console.assert(port.name == "defChannel");
		port.onMessage.addListener(function(msg){
			console.log(msg.query.toString());
			get_definition(msg.query.toString());
		});
	});
	function get_definition(text){
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4){
				var definitionText = xmlhttp.responseText;
				console.log("Received response");
				console.log(definitionText);
				chrome.tabs.getSelected(null, function(tab){
					var port = chrome.tabs.connect(tab.id, {name: "defResponse"});
					console.log("Sending message");
					console.log(definitionText.toString());
					port.postMessage({text: definitionText});
				});

			//	document.getElementById("test").innerHTML = "jou";
			}
		}
	//	console.log("Making request");
		xmlhttp.open("GET", "http://www.google.com/search?q=define:+"+text, true);
		////window.open("http://www.google.com/search?q=define:+"+text);
		xmlhttp.send(null);
	}
</script>
